﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.Diagnostics;
using System.Windows.Forms;
using OpenQA.Selenium.Support.Extensions;

namespace WindowsFormsApp1


/* Main 线程逻辑 Barrier*/
{

    /* 全局类 */
    public class Globle_package
    {
        public static IWebDriver driver;
        public static int IterationNum;
        public static String filmType;
        public static String filmSize; 
    };

    
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
            
        }

      
        
        public static void WebTestInit_Login()
        {
            // init
            IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver();
            Globle_package.driver = driver;
            driver.Navigate().GoToUrl("http://10.78.200.155:5088");
            driver.Manage().Window.Maximize(); // 窗体最大化
            Thread.Sleep(500);
            driver.Navigate().Refresh(); // 刷新页面
            driver.FindElement(By.Id("TextBox1")).Clear();
            driver.FindElement(By.Id("TextBox1")).SendKeys("admin");
            Thread.Sleep(500);
            driver.FindElement(By.Id("TextBox2")).Clear();
            driver.FindElement(By.Id("TextBox2")).SendKeys("123");
            Thread.Sleep(500);
            driver.FindElement(By.Id("Button1")).Click();
            // 定位右侧元素
            var education = driver.FindElement(By.XPath("//*[@id='ContentPlaceHolder1_DropDownList1']"));
            var selectElement_one = new SelectElement(education);
            selectElement_one.SelectByText("识别失败");// 识别失败 人工识别
            var Film_type = driver.FindElement(By.XPath("//*[@id='ContentPlaceHolder1_DropDownList2']"));
            Thread.Sleep(2000);
            var selectElement_two = new SelectElement(Film_type);
            
            selectElement_two.SelectByText(Globle_package.filmType);// PR_aw43 Eleva
            driver.FindElement(By.Id("ContentPlaceHolder1_Button1")).Click();
         
        }
        
        /* 定位元素 */
        public static void Phase1Doing()
        {
          
            IWebDriver driver = Globle_package.driver;
            IList<IWebElement> listOption = driver.FindElements(By.CssSelector("#form1  div.C  div.Left  table  tbody  tr"));
            Array web = listOption.ToArray();
            int a = 0;
            while (a <= web.Length - 2)
            {
                Console.WriteLine("a 的值： {0}", a);
                OCR_Function(a,Globle_package.filmType);
                a++;
            }
            if (web.Length-2 == -1)
            {
                driver.Quit();
            }
            driver.Quit();
          
        }

       


        private static void OCR_Function(int sum_len, string Film_type)
        {
           try
            {
                
                Console.WriteLine("Try 代码运行");
                IWebDriver driver = Globle_package.driver;
                // 获取film_size #form1 > div.C > div.Left > table > tbody > tr:nth-child(3) > td:nth-child(4)
                Thread.Sleep(3000);
                var text = driver.FindElement(By.CssSelector("#form1 div.C div.Left table tbody tr:nth-child(" + (sum_len + 2) + ") td:nth-child(4)")).Text;
                Debug.WriteLine("Text", text); // 11INX14IN  14INX17IN
                Globle_package.filmSize = text;
                driver.FindElement(By.Id("ContentPlaceHolder1_DataList1_lbtDelete_" + sum_len)).Click();
                var all_pages = driver.WindowHandles; // 获取所有句柄
                driver.SwitchTo().Window(all_pages.Last());
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                Thread.Sleep(7000);
                Screenshot_JudgmentTools SJ = new Screenshot_JudgmentTools();
                var res = SJ.Screen_JudgmentToolsMethods();
                //MessageBox.Show(res);
                Debug.WriteLine("识别结果2", res);

                if (res != "")
                {
                    Console.WriteLine("Progam-识别成功", res);
                    Thread.Sleep(3000);
                    driver.FindElement(By.Id("TextBox1")).Clear();
                    driver.FindElement(By.Id("TextBox1")).SendKeys(res);
                    var btn1 = driver.FindElement(By.Id("Button1"));
                    Thread.Sleep(2500);
                    driver.ExecuteJavaScript("arguments[0].click();", btn1);
                    
                }
                Thread.Sleep(3000);
                driver.Close();
                Thread.Sleep(5000);
                driver.SwitchTo().Window(all_pages.First());
                
            }
            catch (Exception ex)
            {
                   Console.WriteLine("异常输出{0}",ex);
                   Globle_package.driver.Quit();
                   System.Environment.Exit(0);

            }

        }
    }
}
